package com.slofzx.nuoda.evaluation.project.util;

import com.spire.xls.Workbook;
import com.spire.xls.Worksheet;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.io.*;

public class SpireExcelUtil {

    /**
     * 加载模板 获取workbook对象
     * @param templatePath
     * @return
     */
    public static Workbook getWorkbook(ByteArrayOutputStream byteArrayOutputStream) {
        InputStream input=null;
        try {
           input  = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        } catch (Exception e) {
            return null;
        }
        final Workbook workbook = new Workbook();
        workbook.loadFromStream(input);
        return workbook;
    }

    public static void saveExcelRowData(Worksheet worksheet,String[] rowData, Integer rowIndex){
        saveExcelRowData(worksheet,rowData,rowIndex,1);
    }

    /**
     * 填充图片数据生成图片
     * @param workbook
     * @return
     */
    public static void saveExcelRowData(Worksheet worksheet,String[] rowData, Integer rowIndex,Integer colIndex) {
        //从指定单元格开始写入二维数组到工作表
        if (ArrayUtils.isEmpty(rowData)) {
            return ;
        }
        final String[][] strings = new String[1][rowData.length];
        strings[0]=rowData;
        worksheet.insertArray(strings, rowIndex, colIndex);
    }
}
